package com.jiaai.web.xiaoke;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.exception.ServerException;
import com.jiaai.share.api.TShareApi;
import com.jiaai.share.model.TShareVO;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title FamilyController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/admin/share")
public class ShareController {
    @Reference
    private TShareApi tShareApi;
    /**
     * 获取所有的列表
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAll")
    @ResponseBody
    public Object findByAll() throws Exception {
        return RestResultGenerator.genSuccessResult(tShareApi.findByAll());
    }
    /**
     * 获取单个平台
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/findById")
    public Object findById(Integer id) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tShareApi.findById(id));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }

    /**
     * 添加编辑家族
     * @param id
     * @param appkey
     * @param appsecret
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/edit")
    public Object edit(Integer id,String appkey,String appsecret) throws ServerException {
        try {
            tShareApi.edit(id,appkey,appsecret);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }

    /**
     * 修改开关状态
     * @param id
     * @param state
     * @return
     * @throws Exception
     */
    @PostMapping("/changeState")
    @ResponseBody
    public Object changeState(Integer id,Integer state) throws ServerException {
        try {
            tShareApi.changeState(id,state);
            return RestResultGenerator.genSuccessResult("修改成功");
        } catch (Exception e) {
            throw new ServerException("修改失敗");
        }
    }
}
